<?php

class erpapi_logistics_matrix_pdd_request_template extends erpapi_logistics_request_template {

    public function syncStandardTpl() {
        return $this->succ();
    }

    public function syncUserTpl() {
        $this->title = '获取拼多多模板';
        $rs = $this->requestCall(STORE_WAYBILL_STANDARD_TEMPLATE, array());
       
        if($rs['rsp'] == 'succ' && $rs['data']) {
            $data = json_decode($rs['data'], true);
            $rs['data'] = array();
            if($data) {
                foreach ($data as $val) {
                    foreach ($val['standard_templates'] as $sVal) {
                        $templateType = $sVal['standard_waybill_type'] == 1 ? 'standard' : 'user';
                        $outTemplateId = $sVal['standard_template_id'];
                        $rs['data'][] = array(
                            'tpl_index' => $templateType . '-' . $outTemplateId,
                            'cp_code' => $val['wp_code'],
                            'out_template_id' => $outTemplateId,
                            'template_name' => $sVal['standard_template_name'] . '-' . $val['wp_code'] . '(拼多多)',
                            'template_type' => 'pdd_' . $templateType,
                            'template_data' => 'url:' . $sVal['standard_template_url']
                        );
                    }
                }
            }
        } else {
            $rs['data'] = array();
        }
        return $rs;
    }

    public function getUserDefinedTpl($params) {
        $this->title = '获取模板(' . $this->__channelObj->channel['logistics_code'] . ')自定义区域内容';
        $template = kernel::single('logisticsmanager_waybill_pdd')->logistics($this->__channelObj->channel['logistics_code']);
        if(!$params['template_id']) {
           // return $this->succ('没有自定义ID');
        }
        $sdf = array(
            'template_id' => $params['template_id']
        );
        if ($params['template_id']==0){
            $sdf = array(
                'template_id' => $template['template_id'],
            );
        }
        $rs = $this->requestCall(STORE_USER_DEFINE_AREA, $sdf);
        if($rs['rsp'] == 'succ' && $rs['data']) {
            $data = json_decode($rs['data'], true);
            $customResult = $data['datas'];
            $rs['data'] = array();
            if($customResult) {
                foreach ($customResult as $val) {
                    $tplSelect = array();
                    if($val['keys']) {
                        foreach ($val['keys'] as $kVal) {
                            if($kVal['key_name']) {
                                $tplSelect[] = str_replace(array('data.'), '', $kVal['key_name']);
                            }
                        }
                        $rs['data'] = array(
                            'custom_area_id' => $val['custom_area_id'],
                            'custom_area_url' => $val['custom_area_url'],
                            'template_select' => $tplSelect
                        );
                        break;
                    }
                }
            }
        } else {
            $rs['data'] = array();
        }
        return $rs;
    }
}